package com.ruoyi.web.sso.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.web.sso.common.util.BeanUtils;
import com.ruoyi.web.sso.common.util.UuidUtil;
import com.ruoyi.web.sso.domain.DTO.StatusResponse;
import com.ruoyi.web.sso.domain.Performance;
import com.ruoyi.web.sso.domain.PerformanceDetail;
import com.ruoyi.web.sso.domain.Staff;
import com.ruoyi.web.sso.domain.vo.MilepostList;
import com.ruoyi.web.sso.mapper.PerformanceDetailMapper;
import com.ruoyi.web.sso.mapper.PerformanceMapper;
import com.ruoyi.web.sso.mapper.StaffMapper;
import com.ruoyi.web.sso.service.PerformanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author admin
* @description 针对表【performance(绩效评估)】的数据库操作Service实现
* @createDate 2025-02-19 16:19:30
*/
@Service
public class PerformanceServiceImpl implements PerformanceService {

    @Autowired
    private PerformanceDetailMapper performanceDetailMapper;

    @Autowired
    private PerformanceMapper performanceMapper;

    /**
     * 新增或编辑考核 (通过id判断，id为每月的id)
     * @param performanceDetail
     * @return
     */
    @Override
    public StatusResponse saveOrUpdatePerformance(Staff staff, PerformanceDetail performanceDetail) {

        //新增绩效评估
        Performance performance = new Performance();
        String userId = staff.getId();
        performance.setAreaId(staff.getAreaId());
        performance.setStaffId(performanceDetail.getStaffId());
        performance.setYear(performanceDetail.getYear());
        performance.setDeleted(0);
        BigDecimal finalScore = performanceDetail.getFinalScore();
        //判断是几月的打分
        switch (performanceDetail.getMonth()){
            case 1:performance.setJanuary(finalScore);break;
            case 2:performance.setFebruary(finalScore);break;
            case 3:performance.setMarch(finalScore);break;
            case 4:performance.setApril(finalScore);break;
            case 5:performance.setMay(finalScore);break;
            case 6:performance.setJune(finalScore);break;
            case 7:performance.setJuly(finalScore);break;
            case 8:performance.setAugust(finalScore);break;
            case 9:performance.setSeptember(finalScore);break;
            case 10:performance.setOctober(finalScore);break;
            case 11:performance.setNovember(finalScore);break;
            case 12:performance.setDecember(finalScore);break;
        }
        //判断当前年是否已经有打分，有则修改
        Performance performance1 = new Performance();
        performance1.setStaffId(performanceDetail.getStaffId());
        performance1.setYear(performanceDetail.getYear());
        Performance performance2 = performanceMapper.selectByAllKey(performance1);
        //修改
        String id = "";
        if(null!=performance2){
            id = performance2.getId();
            BeanUtils.copyProperties(performance,performance2);
            performance2.setId(id);
            performance2.setUpdateUser(userId);
            performanceMapper.updateByPrimaryKeySelective(performance2);
        }
        else{
            id = UuidUtil.getUuid();
            performance.setId(id);
            performance.setCreateUser(userId);
            performanceMapper.insert(performance);
        }

        //判断当月是否是重复打分，重复打分则做修改
        PerformanceDetail performanceDetail1 = new PerformanceDetail();
        performanceDetail1.setMonth(performanceDetail.getMonth());
        performanceDetail1.setYear(performanceDetail.getYear());
        performanceDetail1.setStaffId(performanceDetail.getStaffId());
        PerformanceDetail performanceDetail2 = performanceDetailMapper.selectByAllKey(performanceDetail1);
        if(null!=performanceDetail2){
            performanceDetail.setId(performanceDetail2.getId());
        }

        //通过id判断是新增还是编辑
        if(null!=performanceDetail.getId()){

            performanceDetail.setUpdateUser(userId);
            performanceDetail.setDeleted(0);
            int i = performanceDetailMapper.updateByPrimaryKeySelective(performanceDetail);

            return i>0?StatusResponse.success():StatusResponse.undefined();
        }else{
            performanceDetail.setPerformanceId(id);
            performanceDetail.setCreateUser(userId);
            performanceDetail.setId(UuidUtil.getUuid());
            performanceDetail.setDeleted(0);
            int i = performanceDetailMapper.insert(performanceDetail);

            return i>0?StatusResponse.success():StatusResponse.undefined();
        }
    }

    /**
     * 详情
     * @param month 月份
     * @param year 年份
     * @param staffId 考核对象id
     * @return
     */
    @Override
    public String detail(Integer month, Integer year, String staffId) {
        PerformanceDetail performanceDetail1 = new PerformanceDetail();
        performanceDetail1.setMonth(month);
        performanceDetail1.setYear(year);
        performanceDetail1.setStaffId(staffId);
        PerformanceDetail performanceDetail = performanceDetailMapper.selectByAllKey(performanceDetail1);

        return JSON.toJSONString(performanceDetail);
    }

    /**
     * 列表查询
     * @param year
     * @param staffName
     * @return
     */
    @Override
    public PageImpl<Performance> performanceList(Integer pageNum, Integer pageSize,Integer year, String staffName,String areaId) {

        Map map = new HashMap();
        map.put("year",year);
        map.put("staffName",staffName);
        map.put("areaId",areaId);
        List<Performance> performances = performanceMapper.performanceList(map);
        Integer count = 0;
        if(null!=performances && performances.size()>0){
            count = performances.size();
        }

        PageImpl<Performance> page = new PageImpl(performances, PageRequest.of(pageNum, pageSize), count);

        return page;
    }

    /**
     * 删除
     * @return
     */
    @Override
    public StatusResponse removePerformance(String id) {

        performanceMapper.deleteByPrimaryKey(id);

        performanceDetailMapper.deleteByPrimaryAllKey(id);

        return StatusResponse.success();
    }


}
